<?php
class ManageQuestionsModel extends Model
{
    function  __construct()
    {
        parent::__construct();
    }

    public function getSubjectList($lectureID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT s.id AS id, s.name AS name FROM subjects AS s , classes AS c WHERE s.id = c.subjectid AND lectureid LIKE '%$lectureID%'" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getSectionList($subjectID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT s.id AS id, s.name AS name, s.weight AS weight FROM sections AS s WHERE subjectid LIKE '%$subjectID%'" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getQuestionList($sectionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT q.id AS id, q.content AS content, q.weight AS weight FROM questions AS q WHERE sectionid LIKE '%$sectionID%' ORDER BY id DESC" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getQuestion($questionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT q.id AS id, q.content AS content, q.weight AS weight FROM questions AS q WHERE id = '$questionID'" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getOptionList($questionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT o.id AS id, o.content AS content, o.trueorfalse AS trueorfalse FROM options AS o WHERE questionid LIKE '%$questionID%' ORDER BY id DESC" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getOption($optionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT o.id AS id, o.content AS content, o.trueorfalse AS trueorfalse FROM options AS o WHERE o.id = '$optionID'" );
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    //them, sua, xoa
    public function saveNewQuestion($sectionID, $contentQuestion, $weightQuestion)
    {
        //them cau hoi moi
        $db = DataProvider::getInstance();
        $rs = $db->query("INSERT INTO questions(sectionid, content, weight) VALUES('$sectionID', '$contentQuestion', '$weightQuestion')" );
        if($rs)
            return mysql_insert_id();
        else
            return -1;
    }

    public function updateQuestion($questionID, $sectionID, $contentQuestion, $weightQuestion)
    {
        //them cau hoi moi
        $db = DataProvider::getInstance();
        $rs = $db->query("UPDATE questions SET sectionid='$sectionID', content ='$contentQuestion' , weight='$weightQuestion' WHERE id='$questionID'");
        if($rs)
            return 1;
        else
            return 0;
    }

    public function saveNewOption($questionID, $contentOption, $trueOrFalse)
    {
        //them cau hoi moi
        $db = DataProvider::getInstance();
        $rs = $db->query("INSERT INTO options(questionid, content, trueorfalse) VALUES('$questionID', '$contentOption', '$trueOrFalse')" );
        if($rs)
            return mysql_insert_id();
        else
            return -1;
    }

    public function updateOption($optionID, $questionID, $contentOption, $trueOrFalse)
    {
        //them cau hoi moi
        $db = DataProvider::getInstance();
        $rs = $db->query("UPDATE options SET questionid='$questionID', content ='$contentOption' , trueorfalse='$trueOrFalse' WHERE id='$optionID'");
        if($rs)
            return 1;
        else
            return 0;
    }

    public function deleteOptionList($questionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("DELETE FROM options WHERE questionid='$questionID'" );
        if($rs)
            return 1;
        else
            return 0;
    }

    public function deleteQuestion($questionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("DELETE FROM questions WHERE id='$questionID'" );
        if($rs)
            return 1;
        else
            return 0;
    }
    
    public function deleteOption($optionID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("DELETE FROM options WHERE id='$optionID'" );
        if($rs)
            return 1;
        else
            return 0;
    }
    


}
?>